package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.exception.ErrorCode;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomAreaMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomArea;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author Feng
 * @date 2023/5/6
 */
@Repository
@RequiredArgsConstructor
public class RoomAreaRepository extends ServiceImpl<RoomAreaMapper, RoomArea> {

    public List<String> distinctDistrict(String city) {
        LambdaQueryWrapper<RoomArea> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomArea::getCity, city);
        List<String> list = this.baseMapper.distinct(wrapper, "district");
        return list;
    }

    public List<String> distinctBlock(String city, String district) {
        LambdaQueryWrapper<RoomArea> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomArea::getCity, city)
                .eq(RoomArea::getDistrict, district);
        List<String> list = this.baseMapper.distinct(wrapper, "block");
        return list;
    }

    public List<RoomArea> list(String city, String district, String street, String name) {
        LambdaQueryWrapper<RoomArea> wrapper = Wrappers.lambdaQuery();
        wrapper.select(RoomArea::getName, RoomArea::getCity, RoomArea::getDistrict, RoomArea::getStreet, RoomArea::getAddress, RoomArea::getGeoLat, RoomArea::getGeoLng)
                .eq(StrUtil.isNotEmpty(city), RoomArea::getCity, city)
                .eq(StrUtil.isNotEmpty(district), RoomArea::getDistrict, district)
                .eq(StrUtil.isNotEmpty(street), RoomArea::getStreet, street)
                .and(StrUtil.isNotEmpty(name), e -> {
                    e.like(RoomArea::getName, name)
                            .or()
                            .like(RoomArea::getEname, name);
                });
        return list(wrapper);
    }

    public RoomArea get(String city, String district, String street, String name, String address) {
        LambdaQueryWrapper<RoomArea> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomArea::getCity, city)
                .eq(RoomArea::getDistrict, district)
                .eq(RoomArea::getStreet, street)
                .eq(RoomArea::getName, name)
                .eq(RoomArea::getAddress, address);
        return getOne(wrapper);
    }

    public RoomArea myGetById(Long roomAreaId) {
        RoomArea roomArea = this.getById(roomAreaId);
        if(ObjectUtil.isEmpty(roomArea)){
            String msg = StrUtil.format("RoomAreaId:{}信息不存在",roomAreaId);
            throw new ServiceException(new ErrorCode(30037, msg));
        }
        return roomArea;
    }
}
